<template>
  <div class="message">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="box-card">
          <div class="boxcard-head">
            <el-form :inline="true" class="demo-form-inline">
              <el-form-item>
                <el-button type="primary" icon="el-icon-plus" @click="dialogAdd()">新增</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div class="boxcard-body">
            <el-table
              :data="list"
              style="width: 100%"
              row-key="id"
              :header-cell-style="{ 'background-color': '#EEEEEE' }"
            >
              <el-table-column prop="title" label="消息名称" show-overflow-tooltip></el-table-column>
              <el-table-column prop="message" label="消息" show-overflow-tooltip></el-table-column>
              <el-table-column prop="sendTime" label="发送时间" show-overflow-tooltip></el-table-column>
              <el-table-column prop="status" label="状态" show-overflow-tooltip>
                <template slot-scope="scope">
                  <el-tag>{{ getStatus(scope.row.status) }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="type" label="发送的类型" show-overflow-tooltip>
                <template slot-scope="scope">{{ getType(scope.row.type) }}</template>
              </el-table-column>
              <el-table-column prop="specificType" label="子发送类型" show-overflow-tooltip>
                <template slot-scope="scope">{{ getSpecificType(scope.row.specificType) }}</template>
              </el-table-column>
              <el-table-column prop="runTime" label="实际执行时间" show-overflow-tooltip></el-table-column>
              <el-table-column prop="totalCount" label="总计发送条数" show-overflow-tooltip></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button @click="dialogField(scope.row)" type="text" size="small">编辑</el-button>
                  <el-button @click="delete(scope.row)" type="text" size="small">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="pagination">
              <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="pageChange"
                :total="total"
                :current-page.sync="pageNum"
                :page-sizes="[10, 20]"
                :page-size="10"
                layout="total, sizes, prev, pager, next"
              ></el-pagination>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <edit ref="messageEdit" v-bind:visible.sync="messageEditDialog" @success="getList"></edit>
  </div>
</template>

<script>
import { get, postJson, postXform } from "@/utils/request.js";
import edit from "./edit";

export default {
  components: { edit },
  data() {
    return {
      // 分页参数
      pageNum: 1, // 当前页数
      pageSize: 10, // 页大小
      total: 0, // 数据总条数
      list: [], // 数据列表

      // 弹窗相关属性
      messageEditDialog: false,
      // 数据字典
      statuss: [
        { key: 0, label: "未执行" },
        { key: 1, label: "已执行" },
        { key: 2, label: "执行失败" }
      ],
      types: [
        { key: 1, label: "按客户类型发送" },
        { key: 2, label: "按客户来源发送" },
        { key: 3, label: "主动选择客户" },
        { key: 4, label: "输入手机号" },
        { key: 5, label: "最近三月无消费" },
        { key: 6, label: "最近三月有消费" },
      ],
      specificTypes: [
        { key: 1, label: "客户" },
        { key: 2, label: "医生" },
        { key: 3, label: "员工" },
        { key: 4, label: "分销商" },
        { key: 5, label: "筛查活动" },
        { key: 6, label: "门店" },
        { key: 7, label: "医生" },
        { key: 8, label: "分销商" },
        { key: 9, label: "客户" },
        { key: 10, label: "其他" }
      ],
      smsTemplateList: []
    };
  },
  watch: {},
  created() {
    this.getList(this.pageNum, this.pageSize);
  },
  methods: {
    getStatus(status) {
      for (let i = 0; i < this.statuss.length; i++) {
        if (this.statuss[i].key === status) {
          return this.statuss[i].label;
        }
      }
      return "";
    },
    getType(status) {
      if (status) {
        for (let i = 0; i < this.types.length; i++) {
          if (this.types[i].key === status) {
            return this.types[i].label;
          }
        }
      }
      return "";
    },
    getSpecificType(status) {
      if (status) {
        for (let i = 0; i < this.specificTypes.length; i++) {
          if (this.specificTypes[i].key === status) {
            return this.specificTypes[i].label;
          }
        }
      }
      return "";
    },
    // 分页相关方法
    // 页大小改变时
    handleSizeChange(val) {
      this.pageSize = val;
    },
    // 页码改变时
    pageChange(e) {
      // 获取某页数据
      this.pageNum = e;
      this.getList();
    },
    // 获取列表数据
    getList(num, size) {
      get("/sendMessage/page", {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }).then(res => {
        this.list = res.data.data.list;
        // 自定义下标
        this.list.forEach((e, idx) => {
          e.idx = (idx + 1) * this.pageNum;
        });
        this.total = Number.parseInt(res.data.data.total);
        this.pageNum = res.data.data.pageNum;
      });
    },
    delete(e) {
      postXform("/sendMessage/delete", { id: e.id }).then(res => {
        this.$message({ message: "删除成功", type: "success" });
        this.getList();
      });
    },
    dialogAdd() {
      this.messageEditDialog = true;
      this.$refs.messageEdit.initEdit();
    },
    // 编辑按钮打开编辑字段弹窗
    dialogField(e) {
      this.messageEditDialog = true;
      this.$refs.messageEdit.initEdit(e);
    }
  }
};
</script>

<style lang="less"></style>
